package com.jiangnanxiaoguan.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jiangnanxiaoguan.common.Result;
import com.jiangnanxiaoguan.entity.Category;
import com.jiangnanxiaoguan.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 菜品及套餐分类 前端控制器
 * </p>
 *
 * @author 不愿透露姓名的狂热者
 * @since 2022-09-11
 */
@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 新增菜品分类。
     * @param category
     * @return
     */
    @PostMapping
    public Result<String> save(@RequestBody Category category) {
        log.info(category.toString());
        categoryService.save(category);
        return Result.success("");
    }


    /**
     * 菜品分类分页查询。
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public Result<IPage<Category>> page(Integer page, Integer pageSize) {
        log.info("[菜品/套餐分类分页查询] - 查询页码：{}, 查询条数：{}", page, pageSize);
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(Category::getSort);
        IPage<Category> categoryPage = new Page<>(page, pageSize);
        categoryService.page(categoryPage, queryWrapper);
        return Result.success(categoryPage);
    }


    /**
     * 根据菜品分类id删除菜品。
     * @param id
     * @return
     */
    @DeleteMapping
    public Result<String> delete(Long id) {
        log.info("删除id为 {} 的分类。", id);
        categoryService.remove(id);
        return Result.success("删除分类成功");
    }


    /**
     * 修改菜品分类。
     * @param category
     * @return
     */
    @PutMapping
    public Result<String> update(@RequestBody Category category) {
        log.info("更新分类信息：{}", category);
        categoryService.updateById(category);
        return Result.success("更新分类成功");
    }


    /**
     * 查询菜品分类菜品/套餐菜品。
     * @param type
     * @return
     */
    @GetMapping("/list")
    public Result<List<Category>> list(Integer type) {
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(type != null, Category::getType, type);
        List<Category> list = categoryService.list(queryWrapper);
        return Result.success(list);
    }
}

